import os
import pandas as pd

def excel_to_csv(excel_file, csv_file):
    try:
        df = pd.read_excel(excel_file)
        df.to_csv(csv_file, index=False)
    except Exception as e:
        print(f"Error converting {excel_file} to CSV: {e}")

def convert_and_copy_excel_to_csv(src_dir, dest_dir):
    # 如果目标目录不存在，则创建它
    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)
    
    # 遍历源目录中的所有文件
    for filename in os.listdir(src_dir):
        if filename.endswith(".xlsx") or filename.endswith(".xls"):
            excel_file = os.path.join(src_dir, filename)
            csv_filename = os.path.splitext(filename)[0] + ".csv"
            csv_file = os.path.join(dest_dir, csv_filename)
            
            # 将Excel文件转换为CSV文件
            excel_to_csv(excel_file, csv_file)
            print(f"Converted {excel_file} to {csv_file}")

if __name__ == "__main__":
    src_dir = "/Users/juphoon/project/SoulslikeTRPGTemplate/Assets/Configs/Excel"  # 源目录路径
    dest_dir = "/Users/juphoon/project/SoulslikeTRPGTemplate/Assets/Configs/CSV"  # 目标目录路径
    
    convert_and_copy_excel_to_csv(src_dir, dest_dir)
